package csl.mq.rabbitmq;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import csl.mq.rabbitmq.utils.RabbitMQUtils;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @Description: TODO 生产者
 * @author: 隔山海
 * @date: 2021年08月30日 21:44
 */
@SpringBootTest
@Slf4j
public class Producer {

    /**
     * 测试直连式生产者
     */
    @Test
    public void sendMessage() throws IOException, TimeoutException {
//        创建连接工厂并获取连接
        Connection connection = RabbitMQUtils.initRabbitMQ();
//        创建通道
        Channel channel = connection.createChannel();
        /*
        * 绑定队列
        * 参数1：列队名
        * 参数2：是否持久化
        * 参数3：是否独享
        * 参数4：是否自动删除
        * 参数5：其他参数
        * */
        channel.queueDeclare("test",true,false,false,null);
        /*
        * 发送消息
        * 参数1：交换机名称 暂时用不到
        * 参数2：队列名
        * 参数3：基础参数
        * 参数4：消息的内容
        * */
        channel.basicPublish("","test",null,"test rabbitmq".getBytes());
//        关闭资源
        log.info("消息发送成功！");
        RabbitMQUtils.closeRabbitMQ(connection,channel);
    }

    /**
     * 测试work queue生产者
     */
    @Test
    public void sendMessageWorkQueue() throws IOException, TimeoutException {
        Connection connection = RabbitMQUtils.initRabbitMQ();
        Channel channel = connection.createChannel();
        channel.queueDeclare("work-queue",false,false,false,null);
        for (int i = 0; i < 1000; i++) {
            channel.basicPublish("","work-queue",null,("my word queue--"+i).getBytes());
        }
        log.info("消息发送成功！");
        RabbitMQUtils.closeRabbitMQ(connection,channel);
    }

    /**
     * 测试fanout生产者
     */
    @Test
    public void sendMessageFanout() throws IOException, TimeoutException {
        Connection connection = RabbitMQUtils.initRabbitMQ();
        Channel channel = connection.createChannel();
        /*
        * 参数1：交换机名称
        * 参数2：交换机类型
        * */
        channel.exchangeDeclare("logs", BuiltinExchangeType.FANOUT);
        /*向交换机发消息*/
        channel.basicPublish("logs","",null,"fanout形式的消息".getBytes());
        log.info("消息发送成功！");
        RabbitMQUtils.closeRabbitMQ(connection,channel);
    }

    /**
     * 测试 Routing direct生产者
     */
    @Test
    public void sendMessageRoutingDirect() throws IOException, TimeoutException {
        Connection connection = RabbitMQUtils.initRabbitMQ();
        Channel channel = connection.createChannel();
        String exChangeName = "routing-key";
        channel.exchangeDeclare(exChangeName,BuiltinExchangeType.DIRECT);
        /*声明一个路由*/
        String routingKey = "info";
        channel.basicPublish(exChangeName,routingKey,null,("直连类型交换机消息："+routingKey).getBytes());
        System.err.println("消息发送成功！");
        RabbitMQUtils.closeRabbitMQ(connection,channel);
    }

    /**
     * 测试RoutingTopic生产者
     */
    @Test
    public void sendMessageRoutingTopic() throws IOException, TimeoutException {
        Connection connection = RabbitMQUtils.initRabbitMQ();
        Channel channel = connection.createChannel();
        String exChangeName = "topic";
        channel.exchangeDeclare(exChangeName,BuiltinExchangeType.TOPIC);
        /*声明一个路由*/
        String routingKey = "user.add.insert";
        channel.basicPublish(exChangeName,routingKey,null,("topic 类型的交换机消息,routingKey："+routingKey).getBytes());
        System.err.println("消息发送成功！");
        RabbitMQUtils.closeRabbitMQ(connection,channel);
    }
}
